﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using studentskicentarETFa;
using System.Windows.Media.Animation;
using System.Timers;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
       
        
        private System.Windows.Threading.DispatcherTimer popupTimer;
        private System.Windows.Threading.DispatcherTimer popupTimer1;
        private System.Windows.Threading.DispatcherTimer popupTimer2;
        private System.Windows.Threading.DispatcherTimer popupTimer3;
        private System.Windows.Threading.DispatcherTimer popupTimer4;
        private System.Windows.Threading.DispatcherTimer popupTimer5;
        private System.Windows.Threading.DispatcherTimer popupTimer6;
        private System.Windows.Threading.DispatcherTimer popup;

        public MainWindow()
        {
            InitializeComponent();

               
           
        }

        private void DispatcherTimerSetup()
        {
            popupTimer = new System.Windows.Threading.DispatcherTimer();
            popupTimer.Tick += new EventHandler(popupTimer_Tick);
            popupTimer.Interval = new TimeSpan(0, 0, 0);
            popupTimer.Start();
        }

    
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (UsernameTextBox.Text == "" || PasswordBoxGlavna.Password == "")
                System.Windows.Forms.MessageBox.Show("Unesite trazene podatke!");
            else
            {
                DispatcherTimerSetup();
                popupTimer1 = new System.Windows.Threading.DispatcherTimer();
                popupTimer2 = new System.Windows.Threading.DispatcherTimer();
                popupTimer3 = new System.Windows.Threading.DispatcherTimer();
                popupTimer4 = new System.Windows.Threading.DispatcherTimer();
                popupTimer5 = new System.Windows.Threading.DispatcherTimer();
                popupTimer6 = new System.Windows.Threading.DispatcherTimer();
                popup = new System.Windows.Threading.DispatcherTimer();
            }
        }
        private void popupTimer_Tick(object sender, EventArgs e)
        {

            UsernameLabel.Visibility = Visibility.Hidden;
            UsernameTextBox.Visibility = Visibility.Hidden;
            PasswordLabel.Visibility = Visibility.Hidden;
            PasswordBoxGlavna.Visibility = Visibility.Hidden;
            prijavaButton.Visibility = Visibility.Hidden;
            MainSlika.Visibility = Visibility.Hidden;
            Canvas.Visibility = Visibility.Visible;
            loading.Visibility = Visibility.Visible;
                
            /*Timeline.DesiredFrameRateProperty.OverrideMetadata(typeof(Timeline),new FrameworkPropertyMetadata { DefaultValue = 6});*/
            popupTimer.Stop();
            studentskicentarETFa.DAL d = studentskicentarETFa.DAL.Instanca;
            d.kreirajKonekciju("localhost", "Centar", "root", "");
            studentskicentarETFa.DAL.ZaposlenikDAO z = d.getDAO.getZaposlenikDAO();
            List<Zaposlenik> p = new List<Zaposlenik>();
            p = z.getAll();
            bool da = false;

            foreach (Zaposlenik zd in p)
            {
                string x = zd.Ime[0] + zd.Prezime;


                if (UsernameTextBox.Text == (zd.Ime[0] + zd.Prezime).ToLower() && zd.ID_Kartice1 == PasswordBoxGlavna.Password)
                {
                    da = true;
                    if (zd.Pozicija == "Upravnik")
                    {
                        popupTimer1.Tick += new EventHandler(popupTimer1_Tick);
                        popupTimer1.Interval = new TimeSpan(0, 0, 3);
                        popupTimer1.Start();
                       
                    }
                    else if (zd.Pozicija == "Recepcioner")
                    {
                        popupTimer2.Tick += new EventHandler(popupTimer2_Tick);
                        popupTimer2.Interval = new TimeSpan(0, 0, 3);
                        popupTimer2.Start();
                       
                    }
                    else if (zd.Pozicija == "Računovođa")
                    {
                        popupTimer3.Tick += new EventHandler(popupTimer3_Tick);
                        popupTimer3.Interval = new TimeSpan(0, 0, 3);
                        popupTimer3.Start();
                    }
                    else if (zd.Pozicija == "Instruktor")
                    {
                        popupTimer4.Tick += new EventHandler(popupTimer4_Tick);
                        popupTimer4.Interval = new TimeSpan(0, 0, 3);
                        popupTimer4.Start();
                    }
                    else if (zd.Pozicija == "Radnik u PZR")
                    {

                        popupTimer5.Tick += new EventHandler(popupTimer5_Tick);
                        popupTimer5.Interval = new TimeSpan(0, 0, 3);
                        popupTimer5.Start();
                    }
                    else if (zd.Pozicija == "Koordinator")
                    {
                        popupTimer6.Tick += new EventHandler(popupTimer6_Tick);
                        popupTimer6.Interval = new TimeSpan(0, 0, 3);
                        popupTimer6.Start();
                    }

                }

            }

           if (!da)
            {
                //this.Hide();
                //popupTimer.Stop();
                System.Windows.MessageBox.Show("Nepostojeci korisnik!");
                UsernameLabel.Visibility = Visibility.Visible;
                UsernameTextBox.Visibility = Visibility.Visible;
                PasswordLabel.Visibility = Visibility.Visible;
                PasswordBoxGlavna.Visibility = Visibility.Visible;
                prijavaButton.Visibility = Visibility.Visible;
                MainSlika.Visibility = Visibility.Hidden;
                Canvas.Visibility = Visibility.Hidden;
                loading.Visibility = Visibility.Hidden;
                UsernameTextBox.Text = "";
                PasswordBoxGlavna.Password = "";

               /* popup = new System.Windows.Threading.DispatcherTimer();
                popup.Tick += new EventHandler(popup_Tick);
                this.Hide(); 
                popup.Start();
                */
            }
        }

        private void popup_Tick(object sender, EventArgs e)
        {
            popup.Stop();
            MainWindow m = new MainWindow();
            m.Show();
            


        }

        private void popupTimer6_Tick(object sender, EventArgs e)
        {

            WindowKoordinatorZaInstrukcije w = new WindowKoordinatorZaInstrukcije();
            w.Show();
            popupTimer6.Stop();
            this.Hide();

        }

        private void popupTimer5_Tick(object sender, EventArgs e)
        {

            WPF_Webcam.MainWindow w = new WPF_Webcam.MainWindow();
            w.Show();
            popupTimer5.Stop();
            this.Hide();
        }

        private void popupTimer4_Tick(object sender, EventArgs e)
        {

            WindowInstruktor w = new WindowInstruktor();
            w.Show();
            popupTimer4.Stop();
            this.Hide();
        }

        private void popupTimer3_Tick(object sender, EventArgs e)
        {
            WindowRacunovodja w = new WindowRacunovodja();
            w.Show();
            popupTimer3.Stop();
            this.Hide();
        }

        private void popupTimer2_Tick(object sender, EventArgs e)
        {
            WindowRecepcioner w = new WindowRecepcioner();
            w.Show();
            popupTimer2.Stop();
            this.Hide();
        }

        private void popupTimer1_Tick(object sender, EventArgs e)
        {

            WindowUpravnik w = new WindowUpravnik();
            w.Show();
            popupTimer1.Stop();
            this.Hide();
        }
        

        private void UsernameTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            UsernameTextBox.CharacterCasing = CharacterCasing.Lower;
        }



    }
}
